package org.wattdepot.cli.commands;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Provides the SensorData for a given source.
 * @author Paul Galiza
 * 
 */
public class SensorDataCommand implements Command {
  // For testing the buffer appends
  private int testBufferAppend = 0;

  /**
   * For testing purposes, returns the buffer length.
   * 
   * @return Returns the buffer length.
   */
  public int getTestBufferAppend() {
    return testBufferAppend;
  }

  /**
   * Calls the listSensordata method.
   * 
   * @param userInput The user input.
   * @param client The WattDepotClient
   * @return Returns the method call with the proper parameters.
   */
  public String doCommand(List<String> userInput, WattDepotClient client) {
    return listSensorData(userInput, client);
  }

  /**
   * Returns a string information about a single sensor data instance.
   * 
   * @param commandParam The input from user.
   * @param client The WattDepotClient
   * @return Returns the sensor data information.
   */
  private String listSensorData(List<String> commandParam, WattDepotClient client) {
    StringBuffer buff = new StringBuffer(200);

    // Retrieves the source from the command
    String source = commandParam.get(1);

    // Checks to see if source is valid
    SourceChecker check = new SourceChecker();
    if (!check.sourceChecker(commandParam.get(1), client)) {
      return "Invalid Source";
    }

    try {
      // Checks to see if the time stamp is valid
      TStampChecker timeStamp = new TStampChecker();
      if (!timeStamp.checkTStamp(commandParam.get(3))) {
        return "Invalid timestamp";
      }

      // Creates a time stamp with the user inputed String time stamp
      XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(commandParam.get(3));

      buff.append("Tool: ");
      testBufferAppend++;
      buff.append(client.getSensorData(source, tstamp).getTool());
      testBufferAppend++;
      buff.append("\nSource: ");
      testBufferAppend++;
      buff.append(source);
      testBufferAppend++;
      buff.append("\nProperties: (powerGenerated : ");
      testBufferAppend++;
      buff.append(client.getPowerGenerated(source, tstamp));
      testBufferAppend++;
      buff.append(')');
      testBufferAppend++;

    }
    catch (NotAuthorizedException e) {
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      System.out.println("ERROR: Requested timestamp " + commandParam.get(3) + " for the source "
          + source + " not found.");
      return "";
    }
    catch (BadXmlException e) {
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return buff.toString();
  }
}
